package com.kornero.util.sensor.accelerometr;

import java.util.Arrays;
import java.util.Collection;

public class MathHelper {
    public static float normalize(final float value, final float threshold) {
        return (Math.abs(value) < threshold) ? 0.0f : value;
    }

    public static float average(final Collection<Float> floats) {
        return average(floats, 1.0f);
    }

    public static float average(final Collection<Float> floats, final float multiplexer) {
        float sum = 0;
        for (final float v : floats) {
            sum += v;
        }
        return (sum / floats.size()) * multiplexer;
    }

    public static float[] calcDelta(final float[] previous, final float[] current) {
        if (previous.length != current.length) {
            throw new IllegalArgumentException("Different arrays size:\n" +
                    "previous=" + Arrays.toString(previous) + "\n" +
                    "current=" + Arrays.toString(current));
        }

        final float[] delta = new float[previous.length];
        for (int i = 0; i < delta.length; i++) {
            delta[i] = previous[i] - current[i];
        }
        return delta;
    }

    public static double calcArraySqrt(final float[] values) {
        double sum = 0.0d;
        for (final float value : values) {
            sum += value * value;
        }
        return Math.sqrt(sum);
    }

}